import webview


class Api:
    def __init__(self, window=None):
        self._window = window


    def __dir__(self):
        return [attr for attr in super().__dir__()
                if not attr.startswith('_')]  # 过滤掉私有成员


    def open_file_dialog(self, title="选择文件", file_types=None):
        """使用 pywebview 的 API 打开文件对话框"""
        if file_types is None:
            file_types = ["所有文件 (*.*)"]
        else:
            file_types = [f"{name} ({patterns})" for name, patterns in file_types]

        result = self._window.create_file_dialog(
            webview.OPEN_DIALOG,
            allow_multiple=False,
            directory='',
            save_filename='',
            file_types=file_types
        )
        return result[0] if result else None



    def open_folder_dialog(self, title="选择文件夹"):
        result = self._window.create_file_dialog(
            webview.FOLDER_DIALOG,
            allow_multiple=False,
            directory=''
        )
        return result[0] if result else None


